package org.mat.ipaas.ucenter.service.api.system;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import me.zhyd.oauth.model.AuthCallback;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.service.dto.system.model.ThirdLoginModelDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author scott
 * @since 2018-12-17
 */
@FeignClient(name = "mat-ipaas-ucenter-service", contextId = "third-login-rest-api")
public interface ThirdLoginRestAPI {

    @RequestMapping("/sys/thirdLogin/render/{source}")
    void render(@PathVariable("source") String source) throws IOException;

    @RequestMapping("/sys/thirdLogin/{source}/callback")
    String loginThird(@PathVariable("source") String source, @RequestParam("callback") AuthCallback callback, @RequestParam("modelMap") ModelMap modelMap);

    /**
     * 创建新账号
     *
     * @param model
     * @return
     */
    @PostMapping("/sys/thirdLogin/user/create")
    @ResponseBody
    MatApiResponse<String> thirdUserCreate(@RequestBody ThirdLoginModelDTO model);

    /**
     * 绑定账号 需要设置密码 需要走一遍校验
     *
     * @param json
     * @return
     */
    @PostMapping("/sys/thirdLogin/user/checkPassword")
    @ResponseBody
    MatApiResponse<String> checkPassword(@RequestBody JSONObject json);

    /**
     * 第三方登录回调接口
     *
     * @param token
     * @param thirdType
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/sys/thirdLogin/getLoginUser/{token}/{thirdType}", method = RequestMethod.GET)
    @ResponseBody
    MatApiResponse<JSONObject> getThirdLoginUser(@PathVariable("token") String token, @PathVariable("thirdType") String thirdType) throws Exception;

    /**
     * 第三方绑定手机号返回token
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation("手机号登录接口")
    @PostMapping("/sys/thirdLogin/bindingThirdPhone")
    @ResponseBody
    MatApiResponse<String> bindingThirdPhone(@RequestBody JSONObject jsonObject);

    /**
     * 企业微信/钉钉 OAuth2登录
     *
     * @param source
     * @param state
     * @return
     */
    @ResponseBody
    @GetMapping("/sys/thirdLogin/oauth2/{source}/login")
    String oauth2LoginCallback(@PathVariable("source") String source,
                               @RequestParam("state") String state) throws Exception;

    /**
     * 企业微信/钉钉 OAuth2登录回调
     *
     * @param code
     * @param state
     * @return
     */
    @ResponseBody
    @GetMapping("/sys/thirdLogin/oauth2/{source}/callback")
    String oauth2LoginCallback(
            @PathVariable("source") String source,
            // 企业微信返回的code
            @RequestParam(value = "code", required = false) String code,
            // 钉钉返回的code
            @RequestParam(value = "authCode", required = false) String authCode,
            @RequestParam("state") String state
    );

}